package cn.darksoul3.spark.dataset

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

import scala.beans.BeanProperty

object WCWithDataFrameBySQL {

  def main(args: Array[String]): Unit = {

    val ss: SparkSession = SparkSession.builder().appName("WCWithDataSet").master("local[*]").getOrCreate()
    val sc = ss.sparkContext

    val lines: RDD[String] = sc.textFile("C:\\Users\\cary2\\Desktop\\wordcount.txt")

//    import ss.implicits._
//    val words: DataFrame = lines.map(line => {
//      val fields = line.split("\\s+")
//      Word0(fields(0))
//    }).toDF

//    val words: RDD[Word1] = lines.map(line => {
//      val fields = line.split("\\s+")
//      new Word1(fields(0))
//    })
//    val frame = ss.createDataFrame(words, classOf[Word1])

    val words: RDD[WordByJava] = lines.map(line => {
      val fields = line.split("\\s+")
      new WordByJava(fields(0))
    })
    val frame = ss.createDataFrame(words, classOf[WordByJava])

    frame.show()
    ss.stop()
  }

}

case class Word0(name: String)

class Word1(@BeanProperty val name: String)
